
import { Card } from 'antd';
import { useEffect, useState } from 'react';
const BaseCard = ({ title = "标题", children, request = () => { }, ...props }) => {
    const [loading, setLoading] = useState(true);
    const [list, setList] = useState([])
    useEffect(() => { //发请求
        const req = request();
        if (req instanceof Promise) { //判断是否为promise 对象
            req.then((res) => {
                setLoading(false)
                if (Array.isArray(res.data)) {
                    setList(res.data)
                } else {
                    throw new Error('BaseCard request 返回值不正确')
                }
            })
        } else {
            throw new Error('BaseCard request 返回值必须是promise对象')
        }

    }, []);
    return (


        <Card title={title} {...props} loading={loading} style={{ width: 300, height: "auto", marginBottom: 10 }}>
            {
                list.length ? (
                    list.map((item, index) => {
                        return children(item, index)
                    })
                ) : (
                    <p>暂无数据</p>
                )
            }
        </Card>




    )
}

export default BaseCard;